package com.itheima.web.service.impl.sms;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.itheima.service.pms.IProductService;
import com.itheima.service.pms.ProductSevice;
import com.itheima.service.sms.IFlashPromotionProductRelationService;
import com.itheima.vo.pms.Product;
import com.itheima.vo.sms.FlashPromotionProductRelation;
import com.itheima.vo.sms.FlashPromotionProductVo;
import com.itheima.web.mapper.sms.FlashPromotionProductRelationMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品限时购与商品关系表 服务实现类
 */
@DubboService
public class FlashPromotionProductRelationServiceImpl extends ServiceImpl<FlashPromotionProductRelationMapper, FlashPromotionProductRelation> implements IFlashPromotionProductRelationService {
    @Autowired
    private IFlashPromotionProductRelationService flashPromotionProductRelationService;
    @Autowired
    private ProductSevice productService;

    @Override
    public Page<FlashPromotionProductVo> getList(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
       //先根据条件查询对应的信息
        Page<FlashPromotionProductRelation> pageInfo=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(flashPromotionId!=null,FlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        lqw.eq(flashPromotionSessionId!=null,FlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        Page<FlashPromotionProductRelation> page = flashPromotionProductRelationService.page(pageInfo, lqw);
        List<FlashPromotionProductRelation> list = page.getRecords();
        List<FlashPromotionProductVo> flashPromotionProductVoList=list.stream().map(
                item->{
                    FlashPromotionProductVo flashPromotionProductVo=new FlashPromotionProductVo();
                    BeanUtils.copyProperties(item,flashPromotionProductVo);
                    Long productId = item.getProductId();
                    Product product = productService.getById(productId);
                    flashPromotionProductVo.setProduct(product);
                    return flashPromotionProductVo;
                }
        ).collect(Collectors.toList());
        Page<FlashPromotionProductVo> pageInfo2=new Page<>();
        BeanUtils.copyProperties(pageInfo,pageInfo2,"records");
        pageInfo2.setRecords(flashPromotionProductVoList);
        return pageInfo2;
    }
}
